<?
  session_name('NZSession');
  session_start();
  if (!$_SESSION['indexdone']) header('location:index.php');
  include('data/lib_f_protocol.inc.php');
  
  
  
  /**
  * TODO:
  * Committee Page/dashboard meetings notes page.
  * Finish translating the damn german commented code! 
  * More Detailed membership info so this database is primary source for club, eg competency level, any courses (first aid, teaching kayaking etc...)
  * 
  * EVENTUALLY:
  * Send out a mail if the user has changed their password.
  * Trips Database - working on with Hamish (webmaster from cutc). 
  * TO DISCUSS:
  * Should everybody will be able to create new galleries?
  */
  
  include('data/v_global.inc.php');
  include('data/f_db.inc.php');
  include('data/f_global.inc.php');

  $justloggedin = false;    
  /**
  * Login part
  */
  if (isset($_POST['action'])) {
      if ($_POST['action'] == 'login' && $_POST['email'] && $_POST['pw']) {
        if ($data = mqs("SELECT MID,Name,Rights FROM members WHERE eMail='".$_POST['email']."' AND Password='".md5($_POST['pw'])."'")) {
            session_register('username','logged','MID','rights');
            $_SESSION['username'] = $data['Name'];
            $_SESSION['MID'] = $data['MID'];
            $_SESSION['logged'] = true;
            $_SESSION['rights'] = $data['Rights'];
            settype($_SESSION['rights'],'integer');
            $justloggedin = true;
        }
      # All other actions: logout.
      } else {
            $_SESSION['logged'] = false;
            $_SESSION['MID'] = false;
            $_SESSION['username'] = false;
            $_SESSION['rights'] = 0;
			$_SESSION['topic'] = 'start';
      }
  }
  
  
  include('sites/nav.inc.php');
  
  /**
  * Set $_SESSION['topic']
  * @postcondition: if content does not contain subcontents, the session subcontent variable is set to false.
  *                 see nav.inc.php for contents with subcontents.
  */
  if ($_GET['subtopic']) {
      $_SESSION['subtopic'] = $_GET['subtopic'];
  }
  if ($_GET['topic']) {
      
      $_SESSION['topic'] = $_GET['topic'];
      
      # set default subtopic if not set yet
      if (is_array($subnavs[$_SESSION['topic']])) {
          $hlp = array_keys($subnavs[$_SESSION['topic']]);
          if ($hlp[0] != $_SESSION['subtopic'] && !$_GET['subtopic'])  {
              $_SESSION['subtopic'] = $hlp[0];
          }
      } else if (!$_GET['subtopic']) 
        $_SESSION['subtopic'] = false;
  }
  
  
  
  
  
?>  
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <html>
    <head>
      <title>Welcome to UCCC's Website </title>
      <link rel="stylesheet" type="text/css" href="css/main.php">
      <meta http-equiv="content-type" content="text/html;CHARSET=iso-8859-1">
	  <link rel="shortcut icon" href="./data/favicon.bmp" />
    </head>
    <body>
        <?
          if ($_SESSION['logged']) echo '<script type="text/javascript">
                                     function openOffices() {
                                     offices = window.open("offices.php","Officers","location=no,scrollbars=no,menu=no,dependent=yes,status=no");
                                        if (offices) {
                                            offices.focus();
                                        } else {
                                            //document.write(\'Stuff.\');
                                            alert(\'Please allow Pop-Ups for this site.\');
                                        }
                                     }
                                     '.($justloggedin ? 'openOffices();':'').'
                                   </script>'
        ?>
        <center>
            <div id="mainframe">
                <!-- Header -->
                <div class="box b" id="b_header">
                    <? echo $blue_imgset ?>
                    <div class="box hb" id="hb_header" style="background:url(<? echo BILDERDIR.nav_getHeaderBackground(); ?>)"></div>
                </div>
                <!--
                 <img src="pics/logo.gif" style="position:absolute; left:-5px; top:2px">
                -->
                
                <!-- Navigation  -->
                <div class="box b" id="nav_bluebox">
                    <? echo $blue_imgset ?>
                    navigation
                    <div class="box hb" id="nav_navbox" style="background:url(pics/bg_nav.jpg)"> 
                        <? #echo $brightblue_imgset ?>
                        <? echo ($_SESSION['logged'] ? '':'<br>').nav_getMainNav();?>
                    </div>
                    member login
                    <div class="box hb" id="nav_memberbox" style="background:url(pics/bg_member.jpg)"> 
                        <? #echo $brightblue_imgset ?>
                        <br>
                        online <? echo getOnlineUsers($_SERVER['HTTP_HOST']) ?><br>
                        registered <? echo mqs("SELECT COUNT(MID) FROM members") ?><br>
                        <form action="<? echo $_SERVER['PHP_SELF']?>" method="POST">
                            <table class="box">
                              <tr><td><? echo $_SESSION['logged'] ? '':'<input type="text" class="text" size="17" name="email" value="eMail" onClick="this.value=\'\'">' ?></td></tr> 
                              <tr><td><? echo $_SESSION['logged'] ? '<b>'.$_SESSION['username'].'</b>':'<input type="password" class="text" size="17" name="pw" value="" onClick="this.value=\'\'">' ?></td></tr>
                              <tr><td style="text-align:center"><input type="submit" class="button" size="40" value="<? echo $_SESSION['logged'] ? 'logout':'login' ?>"></td></tr>
                            </table>
                            <input type="hidden" name="action" value="<? echo $_SESSION['logged'] ? 'logout':'login'?>">
                        </form>
                        <? echo $_SESSION['logged'] && ($_SESSION['rights'] & ($r_comittee | $r_webmaster)) ? '<a href="javascript:openOffices()">Open Officers Window</a>':'';?>
                        <br>
                    </div>
                    <font class="stdfont"><? echo date("d/m/y")." - ".getAllHits($_SERVER['HTTP_HOST']); ?></font>   
                </div>
                
                <!-- Content  -->
                <div class="box b" id="con_bluebox">
                    <? echo $blue_imgset;
                       if ($_SESSION['subtopic']) {
                            echo $mainnav[$_SESSION['topic']].'
                                 <div class="box hb" id="con_contentbox" style="background:url(pics/bg_subnav.jpg)">'.
                                    nav_getSubNav().'
                                 </div>';
                       }
                       
                       /**
                       * The include files are expected to assign
                       * a value to the $content variable
                       */                               
                       #$topic = $_SESSION['subtopic'] ? $_SESSION['subtopic']:$_SESSION['topic'];
                       # Security
                       $content = 'Invalid content. Please click <a href="http://'.$_SERVER['HTTP_HOST'].'/~uccc/uccc.php?topic=start" target="_top">here</a> to be forwarded to the start page.';
                       #if ($mainnav[$topic] || in_array($topic,array_keys($subnavs[$_SESSION['topic']]))) {
                       if ($mainnav[$_SESSION['topic']]) {
                           $file = 'sites/'.$_SESSION['topic'].'.inc.php';
                           if (file_exists($file)) {
                             include($file);
                             if (!$_SESSION['logged']) {
                                $content = parse_officer_tags($content);
                             }
                           }
                           else $content = "To create: ".$file;
                       }
                       echo hb_box(nav_getCaption(),$content.'<br><br>');
                    ?>
                </div>
                
                <!-- Eventsbox  -->
                <div class="box b" id="eve_bluebox">
                    <? echo $blue_imgset ?>
                    events
                    <div class="box hb" id="eve_hbbox" style="background:url(pics/bg_eve.jpg)"> 
                        <?
                        $entries = 10;
                        if ($res = mq("SELECT b1.TID,b1.EID,b1.Time,b1.Title,b1.Date
FROM board AS b1
WHERE Type=1 
AND b1.EID = (SELECT MIN(EID) AS meid FROM board as b2 WHERE b1.TID = b2.TID )
GROUP BY TID ORDER BY Date DESC,Time LIMIT $entries")) {
                            echo '<table>';
                            while($data= mysql_fetch_array($res)) {
                                extract($data);
                                echo '<tr><td>'.formatDate($Date).'</td></tr>
                                      <tr><td><a href="'.$_SERVER['PHP_SELF'].'?topic=board&subtopic=events&TID='.$TID.'">'.limitText($Title,17).'</a></td></tr>';
                            }
                            echo '</table>';
                        }
                        ?>
                    </div>
                </div>
        </div>
      </center>
    </body>
  </html>